package huawei.test;

import java.util.Arrays;
import java.util.Scanner;

public class SmartDriver {

    private static int minVal = 0;

    private static int sum = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr = Arrays.stream(sc.nextLine().split(","))
                .mapToInt(Integer::parseInt)
                .toArray();
        int m = arr[0];
        int n = arr[1];
        int[][] map = new int[m][n];
        for (int i = 0; i < m; i++) {
            int[] x = Arrays.stream(sc.nextLine().split(","))
                    .mapToInt(Integer::parseInt)
                    .toArray();
            System.arraycopy(x, 0, map[i], 0, x.length);
        }
        boolean[][] visited = new boolean[m][n];
        dfs(map,visited,0,0);
        if (minVal < -100) {
            System.out.println(-1);
        } else {
            System.out.println(-minVal);
        }
    }

    private static void dfs(int[][] map,boolean[][] visited,int x,int y) {
        if (x <0 || y<0 || x>=map.length || y>=map.length || map[x][y] == 0 || visited[x][y]) {
            return;
        }
        visited[x][y] = true;
        // 终点
        if (x == map.length-1 && y == map[0].length-1) {
            minVal = Math.min(minVal,sum);
            sum-=map[x][y];
            visited[x][y] = false;
            return;
        }

        if (map[x][y] == -1) {
            if (sum > 100) {
                sum+=100;
            } else {
                sum=100;
            }
        } else {
            sum-=map[x][y];
        }
        dfs(map,visited,x-1,y);
        dfs(map,visited,x,y-1);
        dfs(map,visited,x+1,y);
        dfs(map,visited,x,y+1);
        sum+=map[x][y];
        visited[x][y] = false;
    }
}
